#!/bin/bash
#set -x

function error
{
  exit $1
}

NEWLANG=$1

if [ "$1" = "" ] ; then
  echo "ERROR: Null Locale value"
  error 5
fi

#
# customize enviroment for different locales
#
if [ "$NEWLANG" = "ru_RU" ] 
then
#  NEWLANG=ru_RU.koi8r
# Doesn't need koi8r for MCP
  NEWLANG=ru_RU
elif [ "$NEWLANG" = "ja_JP" ] 
then
  NEW_XMODIFIERS="@im=kinput2" 
elif [ "$NEWLANG" = "zh_CN" ]
then
  NEW_XMODIFIERS="@im=xcin-zh_CN" 
elif [ "$NEWLANG" = "zh_TW" ]
then
  NEW_XMODIFIERS="@im=xcin-zh_TW" 
else
  NEW_XMODIFIERS="" 
fi

#
# Update LANG and XMODIFIERS in /etc/SuSEconfig/profile
#
sed "s/LANG=.*$/LANG=\"${NEWLANG}\"/" /etc/SuSEconfig/profile > /var/hsc/tmp/i18n
if [ $? != 0 ] ; then
  echo "ERROR: Could not create locale in /var/hsc/tmp"
  error $?
fi
cp /var/hsc/tmp/i18n /etc/SuSEconfig/profile  2>/dev/null
if [ $? != 0 ] ; then
  echo "ERROR: Could not copy locale file to /etc/SuSEconfig/profile"
  error $?
fi

sed "s/XMODIFIERS=.*$/XMODIFIERS=\"${NEW_XMODIFIERS}\"/" /etc/SuSEconfig/profile > /var/hsc/tmp/i18n
if [ $? != 0 ] ; then
  echo "ERROR: Could not create locale in /var/hsc/tmp"
  error $?
fi
cp /var/hsc/tmp/i18n /etc/SuSEconfig/profile  2>/dev/null
if [ $? != 0 ] ; then
  echo "ERROR: Could not copy locale file to /etc/SuSEconfig/profile"
  error $?
fi

rm /var/hsc/tmp/i18n 2>/dev/null

if [ $? != 0 ] ; then
  echo "ERROR: Could not remove /var/hsc/tmp/i18n"
fi

#if [ "$NEWLANG" = "ru_RU.koi8r" ] ; then
#  grep InputDevice /etc/X11/XF86Config | grep KeyboardRussian
#  if [ $? != 0 ] ; then
#    /opt/hsc/bin/changeRussianKeyboard set
#  fi
#else
#  grep InputDevice /etc/X11/XF86Config | grep KeyboardRussian
#  if [ $? == 0 ] ; then
#    /opt/hsc/bin/changeRussianKeyboard unset
#  fi
#fi

/usr/websm/bin/configPccHtmls -lang $NEWLANG

if [ $? != 0 ] ; then
  echo "ERROR: Could not run /usr/websm/bin/configPccHtmls"
  error 2222
fi

rm -f /opt/hsc/data/keycfgcheck

if [ $? != 0 ] ; then
  echo "ERROR: Unable to remove keyboard config file"
  error 3333
fi

exit 0
